package com.jstyle.jclifexd.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.Activitities;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ActivitvtiesAdapter extends  RecyclerView.Adapter<BaseViewHolder>{
    List<Activitities> Details;
    Context context;
    //私有属性
    private OnItemClickListener onItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(Activitities aa, int position);
    }

    //setter方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public ActivitvtiesAdapter(Context mycontext) {
        this.context = mycontext;
        this.Details = new ArrayList<>();
    }

    public void SetData(List<Activitities> myDetails) {
        this. Details = myDetails;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder( LayoutInflater.from(context).inflate(R.layout.item_activitys, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(final BaseViewHolder holder, final int position) {
        final Activitities sleepDetail = Details.get(position);
        final ViewHolder viewHolder = (ViewHolder) holder;
        Glide.with(context)
                .load(sleepDetail.getUrl())
                .skipMemoryCache(true)// 不使用内存缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE) // 不使用磁盘缓存
                .error(context.getResources().getDrawable(R.drawable.hometitle))
                .into(viewHolder.imageview);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(sleepDetail, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return null==Details?0:Details.size();
    }



    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.item_img)
        ImageView imageview;
        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
